function change_pwd(username, level) {
    if (level >= 10) {
        alert("禁止修改管理员密码！");
        return;
    }
    var pwd = document.getElementById("pwd" + username).value;
    ornot = window.confirm("该密码已修改，确认提交吗？")
    if (ornot == true) {
        $.ajax({
            url: "/FindAndUpdate/UpdateUserByAdmin", //处理页面的路径
            data: {username: username, password: pwd},//要提交的数据是一个JSON
            type: "POST", //提交方式
            //TEXT字符串 JSON返回JSON XML返回XML
            success: function (data) { //回调函数 ,成功时返回的数据存在形参data里
                alert(data);
            }
        });
    }
    else return;

}

function AllInformation() {
    $.ajaxSettings.async = false;
    var countnum = 0;
    $("#user_table").bootstrapTable('destroy');
    $('#user_table').bootstrapTable({
        url: "/FindAndUpdate/FindAllUser",
        method: 'post',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        cache: false,
        //queryParams:queryParams,
        striped: true,
        pagination: true,
        sortable: true,
        sortOrder: "desc",//降序
        sortName: 'level',
        sidePagination: 'client',
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 20, 50, 100, 200, 500],
        search: true,
        strictSearch: false,
        showColumns: true,
        showRefresh: false,
        showExport: false,
        uniqueId: "uid",
        showToggle: false,
        cardView: false,
        // clickToSelect: true,
        //  showFooter:true,
        columns:
            [
                // {field: "checked", checkbox: true},
                {field: "uid", title: "用户id", align: "center", valign: "middle", sortable: "true"},
                {field: "username", title: "用户名", align: "center", valign: "middle", sortable: "true"},
                {
                    field: "password",
                    title: "密码",
                    align: "center",
                    width: 200,
                    valign: "middle",
                    sortable: "true",
                    formatter: AddPwdText
                },
                {
                    field: "level",
                    title: "角色",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    formatter: Addselect
                },
                {
                    field: "email",
                    title: "邮箱",
                    align: "center",
                    valign: "middle",
                    sortable: "true"
                },
                {
                    field: "operation0",
                    title: "权限管理",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    formatter: AddAuthority
                },
                {
                    field: "operation",
                    title: "操作",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    formatter: Adddelete
                },
                {
                    field: "levelnum",
                    title: "权限码值",
                    align: "center",
                    valign: "middle",
                    sortable: "true",
                    visible: false,
                },
            ],
        onLoadSuccess: function (data) {
            if (data.code != null) {
                var code = data.code;
                var message = data.message;
                if (code == "400") {
                    alert(message);
                    window.parent.location.href = "/login.html";
                }
                else if (code == "401") {
                    alert(message);
                    window.location.href = "/error/error.html";
                }
            }
        }
    });

    function AddAuthority(value, row) {
        return [
            '<button class="btn" style="float:right;" onclick="showAuthorityDiv(\'' + row.uid + '\',\'' + row.levelnum + '\')">授权</button>'
        ].join("");
    }

    function Adddelete(value, row) {
        return [
            '<input type="image" src="../../../images/icon/delete.png" style="icon: auto" onclick="DeleteUser(\'' + row.uid + '\',\'' + row.level + '\')">'
        ].join("");
    }

    function Addselect(value, row, index) {
        var role = "普通用户";
        if (value == 2) role = "销售主管";
        else if (value == 3) role = "研发/测试";
        else if (value == 10) role = "管理员";
        return [
            '<select class="custom-select" id="' + row.uid + '"onchange="updatelevel(\'' + value + '\',\'' + row.uid + '\')"> <option selected="selected">' + role + '</option><option value="1">普通用户</option>' +
            '</option><option value="2">销售主管</option>' +
            '</option><option value="3">研发/测试</option>' +
            '</option><option value="10">管理员</option>'
        ].join("");
    }

    function AddPwdText(value, row) {
        return [
            '<input TYPE="text"class="form-control" id="pwd' + row.username + '" value="' + value + '" onchange="change_pwd(\'' + row.username + '\',\'' + row.level + '\')">'
        ].join("");
    }
}

function DeleteUser(uid, level) {
    ornot = window.confirm("确认删除用户？")
    if (ornot == true) {
        if (level >= 10) {
            alert("无法删除管理员用户");
            return;
        }
        $.ajax({
            url: "/FindAndUpdate/DeleteUser",
            data: {uid: uid,},
            type: "POST",
            success: function (data) {
                alert(data);
                if (data == "删除成功！") AllInformation();
            }
        });
    }
    else return;
}

function updatelevel(value, uid) {
    ornot = window.confirm("确认更改？")
    if (ornot == true) {
        if (value == 10) {
            alert("无法更改管理员权限！");
            return;
        }
        var newlevel = document.getElementById(uid).value;
        if (value == newlevel) {
            alert("新权限不能与旧权限相同！");
            return;
        }
        $.ajax({
            url: "/FindAndUpdate/UpdateUserLevel", //处理页面的路径
            data: {uid: uid, level: newlevel},//要提交的数据是一个JSON
            type: "POST", //提交方式
            //TEXT字符串 JSON返回JSON XML返回XML
            success: function (data) { //回调函数 ,成功时返回的数据存在形参data里
                alert(data);
            }
        });
    }
    else return;
}

function showRegisterDiv() {
    document.getElementById("add_user").style.display = "block";
}

var uid, levelnum;
var AtomicEmail = 256;
var AtomicJenkinsList = 16;
var AtomicBuildStop = 32;
var AtomicFeedBack = 64;
var AtomicMessage = 512;
var AtomicReportAll = 1024;

var radio_email = document.getElementsByName("radio_email");
var radio_jenkins_list = document.getElementsByName("radio_jenkins_list");
var radio_build_stop = document.getElementsByName("radio_build_stop");
var radio_feedback = document.getElementsByName("radio_feedback");
var radio_message = document.getElementsByName("radio_message");
var radio_report_all = document.getElementsByName("radio_report_all");

function showAuthorityDiv(temp_uid, temp_levelnum) {
    uid = temp_uid;
    levelnum = temp_levelnum;
    if (IfPermission(AtomicEmail)) radio_email[0].checked = true;
    else radio_email[1].checked = true;
    if (IfPermission(AtomicJenkinsList)) radio_jenkins_list[0].checked = true;
    else radio_jenkins_list[1].checked = true;
    if (IfPermission(AtomicBuildStop)) radio_build_stop[0].checked = true;
    else radio_build_stop[1].checked = true;
    if (IfPermission(AtomicFeedBack)) radio_feedback[0].checked = true;
    else radio_feedback[1].checked = true;
    if (IfPermission(AtomicMessage)) radio_message[0].checked = true;
    else radio_message[1].checked = true;
    if (IfPermission(AtomicReportAll)) radio_report_all[0].checked = true;
    else radio_report_all[1].checked = true;

    document.getElementById("Authority").style.display = "block";
    // alert(uid +  " " +levelnum);
}

function IfPermission(PCode) {
    if (PCode == (levelnum & PCode)) return true;
    else return false;
}

function ChangeAuthority() {

    var radio_email_num = radio_email[0].checked ? radio_email[0].value : radio_email[1].value;
    var radio_jenkins_list_num = radio_jenkins_list[0].checked ? radio_jenkins_list[0].value : radio_jenkins_list[1].value;
    var radio_build_stop_num = radio_build_stop[0].checked ? radio_build_stop[0].value : radio_build_stop[1].value;
    var radio_feedback_num = radio_feedback[0].checked ? radio_feedback[0].value : radio_feedback[1].value;
    var radio_message_num = radio_message[0].checked ? radio_message[0].value : radio_message[1].value;
    var radio_report_all_num = radio_report_all[0].checked ? radio_report_all[0].value : radio_report_all[1].value;


    var codes=radio_email_num+","+radio_jenkins_list_num+","+radio_build_stop_num+","+radio_feedback_num+","+radio_message_num+","+radio_report_all_num;
    $.ajax({
        url: "/FindAndUpdate/update/user/levelnum",
        data: {codes: codes,uid:uid,levelnum:levelnum},
        type: "POST",
        success: function (data) {
            alert(data);
            AllInformation();
            Iclose2();
        }
    });

   // alert(radio_email_num + " " + radio_jenkins_list_num + " " + radio_build_stop_num + " " + radio_feedback_num );


}

function Iclose() {
    document.getElementById("add_user").style.display = "none";
}

function Iclose2() {
    document.getElementById("Authority").style.display = "none";
}

function Add_User() {
    if (!checkpas()) return false;
    var username = document.getElementById("user_name").value;
    var password = document.getElementById("user_password").value;
    var email = document.getElementById("user_email").value;
    //alert(username+" "+password+" "+email);
    $.ajax({
        url: "/LoginAndRegister/registerByAdmin",
        data: {username: username, password: password, email: email},
        type: "POST",
        success: function (data) {
            alert(data);
            if (data == "注册成功!") document.getElementById("add_user").style.display = "none";
        },
        error: function (data) {
            alert("提交失败");
        }
    });
    return false;
}

function checkpas() {//当第一个密码框失去焦点时，触发checkpas2件
    var pas1 = document.getElementById("user_password").value;
    var pas2 = document.getElementById("user_confirm").value;
    if (pas1 != pas2) {
        $(".tip").show();
        return false;
    } else {
        $(".tip").hide();
        return true;
    }
}
